﻿using System;
using System.Drawing;
using System.Windows.Forms;
using SystemWnioskujacy.Exceptions;
using System.Collections.Generic;

namespace SystemWnioskujacy
{
    public partial class ConsoleForm : Form
    {
        List<String> History = new List<string>();
        const int MAX_H = 10;
        int H_INDEX = 0;

        public ConsoleForm()
        {
            InitializeComponent();
            SizeRestrictions();
        }

        private void ConsoleForm_Load(object sender, EventArgs e)
        {
            Base.Test();
            Base.Test2();
        }

        private void SizeRestrictions()
        {
            ConsoleOutputBox.Size = new Size(this.Size.Width, this.Size.Height - 70);
            ConsoleOutputBox.Top = 0;

            ConsoleInputBox.Size = new Size(this.Size.Width, 20);
            ConsoleInputBox.Top = this.Size.Height - 60;
        }

        private void ConsoleForm_Resize(object sender, EventArgs e)
        {
            SizeRestrictions();
        }

        private void ConsoleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing) e.Cancel = true;
        }

        private void ConsoleInputBox_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        try
                        {
                            Parser.Parser.Parse(ConsoleInputBox.Text);
                        }
                        catch (LamesException exc)
                        {
                            this.ConsoleOutputBox.AppendText(exc.Message + "\n");
                        }
                        History.Add(ConsoleInputBox.Text);
                        if (History.Count > MAX_H) History.RemoveAt(0);
                        H_INDEX = History.Count;
                        ConsoleInputBox.Text = String.Empty;
                    }
                    break;
                }
                case Keys.Up:
                {
                    if (H_INDEX != 0)
                    {
                        H_INDEX--;
                        ConsoleInputBox.Text = History[H_INDEX];
                    }
                    break;
                }
                case Keys.Down:
                {
                    if (H_INDEX != MAX_H && H_INDEX < History.Count - 1)
                    {
                        H_INDEX++;
                        ConsoleInputBox.Text = History[H_INDEX];
                    }
                    else if (H_INDEX == History.Count - 1)
                    {
                        H_INDEX++;
                        ConsoleInputBox.Text = String.Empty;
                    }
                    break;
                }
            }
        }
    }
}
